
//  MainViewController.m
//  Video Demo
//
//  Created by cluy on 14-8-11.
//  Copyright (c) 2014年 yangdan. All rights reserved.
//

#import "MainViewController.h"
#import "CollectionCell.h"
#import "PlayVideoViewController.h"
#import "PDF_WebViewController.h"

@interface MainViewController ()

@end

@implementation MainViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
//    在collection view中进行设置（Cell类的注册）
    [self.myCollectionView registerNib:[UINib nibWithNibName:@"CollectionCell" bundle:nil] forCellWithReuseIdentifier:@"CollectionCell"];
}

#pragma mark Video Play
- (void)playVideo
{
    NSString *str = [[NSBundle mainBundle]pathForResource:@"test" ofType:@"mp4"];
    NSURL *url = [NSURL fileURLWithPath:str];
    playControl = [[MPMoviePlayerController alloc]initWithContentURL:url];
    playControl.scalingMode=MPMovieScalingModeAspectFit;
    playControl.controlStyle=MPMovieControlModeHidden;
    playControl.view.frame = self.view.frame;
    [self.view addSubview:playControl.view];
    playControl.fullscreen = YES;
    [playControl play];
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(exitPlay)
                                                 name:MPMoviePlayerPlaybackDidFinishNotification
                                               object:nil];
}
-(void)exitPlay
{
    [self.view removeFromSuperview];
}



#pragma mark CollectionView
//定义展示的UICollectionViewCell的个数
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 4;
}

//定义展示的Section的个数
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 3;
}

//每个UICollectionView展示的内容
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString * CellIdentifier = @"CollectionCell";
    CollectionCell * cell = (CollectionCell *)[collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];
    NSArray *arr = @[@"1.png",@"2.png",@"3.png",@"4.png"];
    NSArray *arr1 =@[@"5.png",@"5.png",@"5.png",@"5.png"];
    if (!cell) {
        cell= (CollectionCell *)[collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];
    }
    if (indexPath.section == 1) {
         cell.imgView.image = [UIImage imageNamed:[arr1 objectAtIndex:indexPath.row]];
    }else
    {
        cell.imgView.image = [UIImage imageNamed:[arr objectAtIndex:indexPath.row]];

    }
       return cell;
}
//set footerview
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section
{
    return CGSizeMake(10, 10);
}
#pragma mark --UICollectionViewDelegate

//UICollectionView被选中时调用的方法
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    CollectionCell * cell = (CollectionCell *)[collectionView cellForItemAtIndexPath:indexPath];
    NSLog(@"indexpath.row = %d",indexPath.row);
    cell.imgView.image = [UIImage imageNamed:@"6.png"];
    
    
    //播放视频
    if (indexPath.section == 1) {
        PlayVideoViewController *playView = [[PlayVideoViewController alloc]init];
        [self presentViewController:playView animated:YES
                         completion:^{
                             
                         }];
    }else //打开文件
    {
        PDF_WebViewController *pdfVC = [[PDF_WebViewController alloc]init];
        [self presentViewController:pdfVC animated:YES
                         completion:^{
                             
                         }];

    }
    
}

//返回这个UICollectionView是否可以被选择
-(BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath
{

    if (indexPath.section == 0) {
        return NO;
    }
    return YES;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
